#include "CInputManager.h"

CInputManager::CInputManager()
{
	m_bKeyWasDown = false;
}
CInputManager::~CInputManager()
{
}

const sMouseState CInputManager::GetMouseState()
{
	sMouseState mouse;

	mouse.m_Pos = m_Mouse.getPosition(*m_Window);

	if(m_Mouse.isButtonPressed(sf::Mouse::Left))
	{
		mouse.m_bPressed = true;
		mouse.m_eButton = eLEFT;
	}
	else if(m_Mouse.isButtonPressed((sf::Mouse::Right)))
	{
		mouse.m_bPressed = true;
		mouse.m_eButton = eRIGHT;
	}
	else
	{
		mouse.m_bPressed = false;
		mouse.m_eButton = eNONE;
	}
	m_sLastKnownState = mouse;
	return mouse;
}

const sMouseState CInputManager::LastKnownState()
{
	return m_sLastKnownState;
}

bool CInputManager::Init(sf::Window &window)
{
	m_Window = &window;
	return true;
}
void CInputManager::Shutdown()
{
}

// THIS FUNCTION WILL CHANGE
// I just wanted to get basic text working... I'll figure out a way to get every key as the correct character so it shows in chat
const sCharKey CInputManager::GetPressedKey()
{
	
	sCharKey key;
	// Only assign the character if we are a text character, or a number, otherwise leave it alone
	key.m_Key = sf::Keyboard::KeyCount;
	key.m_cChar = -1;

	if(m_bKeyWasDown == true)
	{
		return key;
	}

	for(int index = 0; index < sf::Keyboard::KeyCount; index++)
	{
		// Treat the number as the enum it's expecting... since they are the same thing
		if(m_Keyboard.isKeyPressed((sf::Keyboard::Key)index))
		{
			key.m_Key = (sf::Keyboard::Key)index;
			// We are a character, check if we are shifted and we're good
			if(index <= sf::Keyboard::Z)
			{
				if(m_Keyboard.isKeyPressed(sf::Keyboard::LShift) || m_Keyboard.isKeyPressed(sf::Keyboard::RShift))
				{
					key.m_cChar = key.m_Key + 65;
				}
				else
				{
					key.m_cChar = key.m_Key + 97;
				}

			}
			else if(index == sf::Keyboard::Space)
			{
				key.m_cChar = ' ';
			}
			break;
			m_bKeyWasDown = true;
		}
	}

	return key;
}
bool CInputManager::IsKeyDown(sf::Keyboard::Key key)
{
	return m_Keyboard.isKeyPressed(key);
}


void CInputManager::CheckKeyboardToClear()
{
	m_bKeyWasDown = false;
	for(int index = 0; index < sf::Keyboard::KeyCount; index++)
	{
		// Treat the number as the enum it's expecting... since they are the same thing
		if(m_Keyboard.isKeyPressed((sf::Keyboard::Key)index) && index != 38 && index != 42)
		{
			m_bKeyWasDown = true;
			break;
		}
	}
}

char CInputManager::GetCharacterFromKeyCode(sf::Keyboard::Key key)
{
	char toReturn = 'a';

	if(key <= sf::Keyboard::Z)
	{
		if(m_Keyboard.isKeyPressed(sf::Keyboard::LShift) || m_Keyboard.isKeyPressed(sf::Keyboard::RShift))
		{
			toReturn = key + 65;
		}
		else
		{
			toReturn = key + 97;
		}
		return toReturn;
	}
	else if(key <= sf::Keyboard::Num9)
	{
		// 23 is the difference from Num0 to the ascii value of 0 which starts at 48
		return toReturn = key + 23;
	}
	else if(key == sf::Keyboard::Comma)
	{
		return toReturn = ',';
	}
	else if(key == sf::Keyboard::Period)
	{
		return toReturn = '.';
	}
	else if(key == sf::Keyboard::Space)
	{
		return toReturn = ' ';
	}

	return toReturn;
}